package com.kanshu.ksgb.fastread.doudou.module.bookcity.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.FinishedAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.WrapBookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedRenqiFinishedLayout;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wcy on 18/5/22.
 */
@Route(path = ARouterConfig.FINISHED_PAGE)
public class FinishedActivity extends BaseActivity implements IGenrialMvpView<BaseResult<List<SelectedBean>>>{
    @BindView(R.id.recyler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.empty_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;
    BookCityPresenter mPresenter;
    PageRequestParams mRequestParams;
    FinishedAdapter mFinishedAdapter;
    List<WrapBookInfo> mBookInfos = new ArrayList<>();
    AdSelectedRenqiFinishedLayout mFinishedLayout;
    int mTotalPage;
    boolean mSwitch;
    int mSite = Constants.MALE;
    ImageView mImageView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_common_refresh_rv);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        setTitle("完本");
        mImageView = new ImageView(this);
        mImageView.setImageResource(R.mipmap.ic_switch_male);
        mTitle.getRightContainer().addView(mImageView);
        mImageView.setOnClickListener(v -> {
            if (mSite == Constants.MALE) {
                mImageView.setImageResource(R.mipmap.ic_switch_female);
                mSite = Constants.FEMALE;
            } else {
                mImageView.setImageResource(R.mipmap.ic_switch_male);
                mSite = Constants.MALE;
            }
            mFinishedLayout.refresh(mSite);
            mRequestParams.page = 1;
            mPresenter.getSelectFinishedBooks(mRequestParams, String.valueOf(mSite));
        });
        mPresenter = new BookCityPresenter(lifeCyclerSubject);
        SwipeRefreshHelper.init(mSwipeRefresh, () -> {
            mRequestParams.page = 1;
            mPresenter.getSelectFinishedBooks(mRequestParams, String.valueOf(mSite));
        });
        mFinishedLayout = new AdSelectedRenqiFinishedLayout(this);
        mFinishedAdapter = new FinishedAdapter(this, mBookInfos);
        mFinishedAdapter.addHeaderView(mFinishedLayout);
        mFinishedLayout.setBottomChangeCallback(() -> {
            mRequestParams.page++;
            mSwitch = true;
            if (mRequestParams.page > mTotalPage) {
                mRequestParams.page = 1;
            }
            mPresenter.getSelectFinishedBooks(mRequestParams, String.valueOf(mSite));
        });
        RecyclerViewHelper.initRecyclerViewV(this, mRecyclerView, mFinishedAdapter);
        mFinishedAdapter.setRequestDataListener(() -> {
            mPresenter.getSelectFinishedBooks(mRequestParams, String.valueOf(mSite));
        });
        mRequestParams = new PageRequestParams();
        mPresenter.setMvpView(this);
        mPresenter.getSelectFinishedBooks(mRequestParams,String.valueOf(mSite));
        mFinishedLayout.refresh(mSite);
    }

    @Override
    public void showLoading(String s) {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void showContent(BaseResult<List<SelectedBean>> result) {
        if (!BaseResult.isNotNull(result) || Utils.isEmptyList(result.result.data)) {
            return;
        }
        SelectedBean bean = result.result.data.get(0);
        if (mRequestParams.page == 1 || mSwitch) {
            mSwitch = false;
            if (Utils.isEmptyList(bean.list) && mBookInfos.size() <= 0) {
                SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mFinishedAdapter);
                return;
            }
            mBookInfos.clear();
            mFinishedAdapter.setIsNoMoreData(false);
        }
        List<WrapBookInfo> bookInfos = BookCityUtils.WrapBookInfo(Integer.parseInt(bean.page_info.cur_page),mBookInfos,  bean.list, 3, result.result.total_page);
        mBookInfos.addAll(bookInfos);
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
        mTotalPage = bean.page_info.total_page;
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mFinishedAdapter);
        if (mRequestParams.page >= bean.page_info.total_page) {
            mFinishedAdapter.noMoreData();
        } else {
            mRequestParams.page++;
        }
        mFinishedAdapter.notifyDataSetChanged();
        mFinishedLayout.cancelChangeBottomRoate();
    }

    @Override
    public void showError(int errCode, String errDesc) {
        if (mFinishedLayout == null) {
            return;
        }
        mFinishedLayout.cancelChangeBottomRoate();
    }
}
